package homework.newenergycharging.Work03.Mapper;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class Work03_Mapper extends Mapper<LongWritable, Text, Text, DoubleWritable> {
    private final Text outKey = new Text();
    private final DoubleWritable outValue = new DoubleWritable();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 输入数据示例：1366563,7.78,0.0,2014-11-18 15:40:26,2014-11-18 17:11:04,15,17,1.510555556,Tue,android,35897499,582873,461655,0,3,0,1,0,0,0,0,0
        String[] keys = value.toString().split(",");

        // 设置 outKey 为星期几（keys[8]）
        outKey.set(keys[8]);

        // 将 keys[1] 解析为 double 类型，并设置给 outValue
        try {
            double valueDouble = Double.parseDouble(keys[1]);
            outValue.set(valueDouble);
        } catch (NumberFormatException e) {
            // 如果解析失败，可以记录日志或跳过该行
            System.err.println("无法解析的 double 值: " + keys[1]);
            return;
        }

        // 输出键值对
        context.write(outKey, outValue);
    }
}

